package com.Packet
{
	
	import flash.utils.ByteArray;
	import flash.utils.Endian;

	public class PacketEncoder
	{
		private var _byte:ByteArray = new ByteArray();	
		
		public function PacketEncoder(order:Packet)
		{
			_byte.endian =  Endian.LITTLE_ENDIAN;
			
			writeInt(order.type);
			writeInt(order.id);
			
			var L:int = order.body.length;
			for(var i:int = 0; i<L; i++)
			{
				var item:* = order.body[i];
				
				if(item is String) writeString(String(item))
				else if(item is int) writeInt(int(item))
				else if(item is Number) writeNum(Number(item))
				else if(item is ByteArray) writeByte(item as ByteArray)
				else if(item is WString)  writeWstr((item as WString).getStr())	
				else throw new Error("PacketEncoder 参数类型有错误")
			}
		}
		
		
		public function str2Wstr(s:String):ByteArray
		{
			var L:int = s.length;
			var re:ByteArray = new ByteArray();
			
			for(var i:int = 0; i<L; i++)
			{
				var t:int = int(s.charCodeAt(i).toString(10));
				re.writeByte(t & 255);
				re.writeByte(t>>8);
			}
			re.writeByte(0);
			re.writeByte(0);
			return re;
		}
		
		private function writeWstr(s:String):void
		{
			var b:ByteArray = str2Wstr(s);
			
			_byte.writeByte(PacketDataType.TYPE_WIDESTR);
			_byte.writeInt(b.length);
			_byte.writeBytes(b,0,b.length);
			
		}
		
		public function getByte():ByteArray
		{
			return _byte;
		}
		
		private function writeInt(i:int):void
		{
			_byte.writeByte(PacketDataType.TYPE_INT);
			_byte.writeInt(i);
		}
		
		private function writeNum(n:Number):void
		{
			_byte.writeByte(PacketDataType.TYPE_FLOAT);
			_byte.writeFloat(n);
		}
		
		private function writeString(s:String):void
		{
			var b:ByteArray = new ByteArray();// = str2Wstr(s);
			b.writeUTFBytes(s);
			
			_byte.writeByte(PacketDataType.TYPE_STRING);
			//加一位的结束符
			_byte.writeInt(b.length+1);
			_byte.writeBytes(b,0,b.length);
			_byte.writeByte(0);
		}
		
		private function writeByte(b:ByteArray):void
		{
			_byte.writeByte(PacketDataType.TYPE_BINARY);
			_byte.writeInt(b.length);
			_byte.writeBytes(b,0,b.length);
		}
	}
}